package org.javascud.extensions.cxf.zookeeper.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.zookeeper.ZooKeeper;
import org.javascud.extensions.cxf.AbstractLoadBalanceStrategy;
import org.javascud.extensions.cxf.service.Hello;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class HelloClient5Zookeeper {


    public static String zkAddress = "127.0.0.1:2181";
    public static String groupName = "helloservice";

    /**
     * Thread 1: read service list, watch
     * Thread 2: call service
     */
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context
                = new ClassPathXmlApplicationContext(new String[]
                {"org/javascud/extensions/cxf/zookeeper/client/loadbalance_fail_zookeeper.xml"});
        final Hello client = (Hello) context.getBean("helloClient");

        final AbstractLoadBalanceStrategy strategy = (AbstractLoadBalanceStrategy) context.getBean("loadBalanceStrategy");

        Client myclient = ClientProxy.getClient(client);
        String address = myclient.getEndpoint().getEndpointInfo().getAddress();
        System.out.println(address);



/*
        ZooKeeper zk = (ZooKeeper) context.getBean("zooKeeper");

        ServiceEndpointsFetcher fetcher = new ServiceEndpointsFetcher();
        fetcher.setStrategy(strategy);
        fetcher.setZooKeeper(zk);
        fetcher.setGroupName(groupName);
        fetcher.start();
*/
        /* --Watcher code has been moved to xml         */

        //call service for test
        for (int i = 1; i <= 1000; i++) {
            String result1 = client.sayHello("Felix" + i);
            System.out.println("Call " + i + ": " + result1);

            int left = strategy.getAlternateAddresses(null).size();
            System.out.println("================== left " + left + " ===========================");

            Thread.sleep(100);
        }


    }


}
